import { Button, Tooltip } from 'antd';
import { getColorTextLoadMap, onMouseOutBtn, removeDateHmsStr, renderTableText, tableSorter, tooltipLength, priceFormat } from '@/utils';
import { PlusCircleOutlined, DeleteOutlined } from '@ant-design/icons';
import pageStyles from '@/common/pageLayout.less';
import MyIcon from '@/components/MyIcon';

/** 相对方表格 */
const openCounterpartColumns = (
  openConterpartModal: (open: boolean) => void,
  delSelectRow: (resObj: Record<string, any>) => void,
  tableConfigType: boolean, // true  为表单展示     false 为弹框
  setShowInfoDrawer: (payload: Record<string, any>) => void,
  pageType?: string,
) => {
  let tableConfig: Record<string, any>[] = [
    {
      title: '单位名称',
      dataIndex: 'oppositeName',
      key: 'oppositeName',
      visible: true, // false；选择相对方弹框  true:添加页面
      width: 190,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'oppositeName'),
      render: (_text: string | any, record: Record<string, any>) => {
        return (
          <Tooltip title={tooltipLength(record.oppositeName, 50)}>
            {tableConfigType ? (
              <a
                className={pageStyles['over-flow-hidden']}
                onClick={() => {
                  setShowInfoDrawer({
                    isShowDrawer: true,
                    showInfoObj: record,
                  });
                }}
              >
                {record.oppositeName || '--'}
              </a>
            ) : (
              <span className={pageStyles['over-flow-hidden']}>{record.oppositeName || '--'}</span>
            )}
          </Tooltip>
        );
      },
    },
    {
      title: '统一社会信用代码',
      dataIndex: 'creditID',
      key: 'creditID',
      visible: true,
      width: 230,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '开户行名称',
      dataIndex: 'bankName',
      key: 'bankName',
      visible: true,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '开户行账号',
      dataIndex: 'bankAccount',
      key: 'bankAccount',
      visible: true,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },

    {
      title: '法定代表人',
      dataIndex: 'legalName',
      key: 'legalName',
      visible: true,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '授权代表',
    //   dataIndex: 'empowerName',
    //   key: 'empowerName',
    //   visible: true,
    //   width: 180,
    //   render: (text: string) => renderTableText(text, true),
    // },
    // {
    //   title: '授权代表',
    //   dataIndex: 'authName',
    //   key: 'authName',
    //   visible: true,
    //   width: 150,
    //   render: (text: string) => renderTableText(text, true),
    // },

    {
      title: '单位地址',
      dataIndex: 'workAddress',
      key: 'workAddress',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'workAddress'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '联系人',
      dataIndex: 'contactName',
      key: 'contactName',
      visible: false,
      width: 140,
      sorter: (a: any, b: any) => tableSorter(a, b, 'contactName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '联系电话',
      dataIndex: 'phoneMobile',
      key: 'phoneMobile',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'phoneMobile'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单位电话',
      dataIndex: 'registerPhone',
      key: 'registerPhone',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'registerPhone'),
      render: (text: string) => renderTableText(text, true),
    },
  ];

  if (tableConfigType) {
    tableConfig = tableConfig?.filter((item: any) => item?.visible);
    if (pageType != 'detail') {
      tableConfig?.push({
        title: (
          <div style={{ width: '100%', textAlign: 'center' }}>
            <Button
              type="link"
              size="small"
              icon={<PlusCircleOutlined />}
              className="manageBtntTxtStyle"
              onMouseOut={onMouseOutBtn}
              onClick={() => {
                openConterpartModal(true);
              }}
            >
              选择
            </Button>
          </div>
        ),
        dataIndex: 'otherBtn',
        key: 'otherBtn',
        visible: true,
        width: 100,
        fixed: 'right',
        render: (_text: any, record: any) => {
          return (
            <div className="counterpoartDelBtnStyle">
              <Button
                type="link"
                size="small"
                icon={<DeleteOutlined />}
                className={'manageBtntTxtStyle'}
                onMouseOut={onMouseOutBtn}
                onClick={() => {
                  delSelectRow(record);
                }}
              >
                删除
              </Button>
            </div>
          );
        },
      });
    }
  }

  return tableConfig;
};

/** 甲方表格 */
const openPartyColumns = (openPartyModal: () => void, delSelectRow: () => void, pageType?: string) => {
  let tableConfig: Record<string, any>[] = [
    {
      title: '合同主体名称',
      dataIndex: 'oppositeName',
      key: 'oppositeName',
      visible: false, // false；选择相对方弹框  true:添加页面
      width: 190,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'oppositeName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '统一社会信用代码',
      dataIndex: 'creditID',
      key: 'creditID',
      visible: true,
      width: 230,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '开户行名称',
      dataIndex: 'bankName',
      key: 'bankName',
      visible: false,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '开户行账号',
      dataIndex: 'bankAccount',
      key: 'bankAccount',
      visible: true,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单位地址',
      dataIndex: 'workAddress',
      key: 'workAddress',
      visible: false,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'workAddress'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单位电话',
      dataIndex: 'registerPhone',
      key: 'registerPhone',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'registerPhone'),
      render: (text: string) => renderTableText(text, true),
    },
  ];
  if (pageType != 'detail') {
    tableConfig?.push({
      title: (
        <div style={{ width: '100%', textAlign: 'center' }}>
          <Button type="link" size="small" icon={<PlusCircleOutlined />} className="manageBtntTxtStyle" onMouseOut={onMouseOutBtn} onClick={openPartyModal}>
            选择
          </Button>
        </div>
      ),
      dataIndex: 'otherBtn',
      key: 'otherBtn',
      visible: true,
      width: 90,
      fixed: 'right',
      render: (_text: any, record: any) => {
        return (
          <div className="counterpoartDelBtnStyle">
            <Button type="link" size="small" icon={<DeleteOutlined />} className={'manageBtntTxtStyle'} onMouseOut={onMouseOutBtn} onClick={delSelectRow}>
              删除
            </Button>
          </div>
        );
      },
    });
  }
  return tableConfig;
};

/** 采购申请 */
const updateSourceColumns = () => {
  return [
    {
      title: '采购项目名称',
      dataIndex: 'purchProjectName',
      key: 'purchProjectName',
      visible: true,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'purchProjectName'),
      width: 200,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '采购项目编号',
      dataIndex: 'purchProjectCode',
      key: 'purchProjectCode',
      visible: true,
      sorter: (a: any, b: any) => tableSorter(a, b, 'purchProjectCode'),
      width: 200,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '采购方式',
      dataIndex: 'procureRealName',
      key: 'procureRealName',
      visible: true,
      sorter: (a: any, b: any) => tableSorter(a, b, 'procureReal'),
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '采购类别',
      dataIndex: 'supplyWay',
      key: 'supplyWay',
      visible: true,
      sorter: (a: any, b: any) => tableSorter(a, b, 'supplyWay'),
      width: 150,
      render: (text) => {
        const arr = [
          { label: '月度计划', value: 0 },
          { label: '零星采购', value: 1 },
          { label: '实验平台', value: 2 },
        ];
        const list = arr.filter((item) => item.value == text) || [];
        return list.length ? renderTableText(list[0].label, true) : '--';
      },
    },
    {
      title: '中标时间',
      dataIndex: 'bidedTime',
      key: 'bidedTime',
      visible: true,
      sorter: (a: any, b: any) => tableSorter(a, b, 'bidedTime'),
      width: 130,
      render: (text: string) => {
        const dateText: string = removeDateHmsStr(text);
        return <span> {dateText}</span>;
      },
    },
  ];
};

/** 合同范本表格配置 */
const updateTempColumns = (openExolain: (resObj: Record<string, any>) => void) => {
  return [
    {
      title: '范本名称',
      dataIndex: 'fullName',
      key: 'fullName',
      visible: true,
      width: 250,
      fixed: 'left',
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '范本描述',
      dataIndex: 'templateInfo',
      key: 'templateInfo',
      visible: true,
      width: 250,
      // render: (text: string) => renderTableText(text, true),
      render: (text: string, record: Record<string, any>) => {
        return (
          <span>
            <a
              onClick={(e: any) => {
                e?.stopPropagation();
                openExolain(record);
              }}
              style={{ color: 'red' }}
            >
              使用说明
            </a>
          </span>
        );
      },
    },
    {
      title: '合同类型',
      dataIndex: 'businessTypeName',
      key: 'businessTypeName',
      width: 250,
      visible: true,
      sorter: (a: any, b: any) => (a?.templateType > b?.templateType ? 1 : -1),
      render: (text: string | null) => {
        // ------------------------------------------------------------------------------
        const { colorText } = getColorTextLoadMap(text);
        return <span className={pageStyles['over-flow-hidden']}>{colorText ? colorText : '--'}</span>;
      },
    },
  ];
};
/** 合同主体表格配置 */
const contractMainTableColumns = () => {
  return [
    {
      title: '合同主体名称',
      dataIndex: 'oppositeName',
      key: 'oppositeName',
      visible: true,
      width: 180,
      fixed: 'left',
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单位地址',
      dataIndex: 'workAddress',
      key: 'workAddress',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'workAddress'),
      render: (text: string) => renderTableText(text, true),
    },
    // {
    //   title: '联系人',
    //   dataIndex: 'contactName',
    //   key: 'contactName',
    //   visible: false,
    //   width: 140,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'contactName'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    // {
    //   title: '联系电话',
    //   dataIndex: 'phoneMobile',
    //   key: 'phoneMobile',
    //   visible: false,
    //   width: 150,
    //   sorter: (a: any, b: any) => tableSorter(a, b, 'phoneMobile'),
    //   render: (text: string) => renderTableText(text, true),
    // },
    {
      title: '开户行名称',
      dataIndex: 'bankName',
      key: 'bankName',
      visible: true,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '开户行账号',
      dataIndex: 'bankAccount',
      key: 'bankAccount',
      visible: true,
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '统一社会信用代码',
      dataIndex: 'creditID',
      key: 'creditID',
      visible: true,
      width: 230,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '单位电话',
      dataIndex: 'registerPhone',
      key: 'registerPhone',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'registerPhone'),
      render: (text: string) => renderTableText(text, true),
    },
  ];
};

/**渲染物料状态*/
const stateOfRendering = (state: number | null | undefined, isShowIcon: boolean = true) => {
  if (typeof state !== 'number') return '--';
  if (state == 0) {
    return (
      <div>
        {isShowIcon && <MyIcon type="icon-dian" style={{ color: '#6812f2' }} />}&nbsp;
        <span>起草</span>
      </div>
    );
  } else if (state == 1) {
    return (
      <div>
        {isShowIcon && <MyIcon type="icon-dian" style={{ color: '#2390fd' }} />}&nbsp;
        <span>审核中</span>
      </div>
    );
  } else if (state == 2) {
    return (
      <div>
        {isShowIcon && <MyIcon type="icon-dian" style={{ color: '#59d6a5' }} />}&nbsp;
        <span>有效</span>
      </div>
    );
  } else {
    return (
      <div>
        {isShowIcon && <MyIcon type="icon-dian" style={{ color: '#5c6f91' }} />}&nbsp;
        <span>作废</span>
      </div>
    );
  }
};

/** 表格配置 */
const materialTableConfig = () => {
  return [
    {
      title: '物料名称',
      dataIndex: 'fullName',
      key: 'fullName',
      width: 190,
      fixed: 'left',
      sorter: (a: any, b: any) => tableSorter(a, b, 'fullName'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '物料类型',
      dataIndex: 'materialTypeName',
      key: 'materialTypeName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'materialType'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '规格要求（参数）',
      dataIndex: 'specInfo',
      key: 'specInfo',
      width: 180,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '规格型号',
      dataIndex: 'modelNum',
      key: 'modelNum',
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '物料状态', //0：起草 1：审核中 2：有效 3：作废
      dataIndex: 'state',
      key: 'state',
      visible: true,
      width: 150,
      unDrag: false,
      sorter: (a: any, b: any) => (a?.state > b?.state ? 1 : -1),
      render: (text: number | null) => stateOfRendering(text),
    },
    {
      title: '供货分类',
      dataIndex: 'supplyTypeName',
      key: 'supplyTypeName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'supplyType'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '量化单位',
      dataIndex: 'unitBusinessName',
      key: 'unitBusinessName',
      visible: true,
      width: 150,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitBusiness'),
      render: (text: string) => renderTableText(text, true),
    },
    {
      title: '参考单价（元）',
      dataIndex: 'unitPrice',
      key: 'unitPrice',
      visible: true,
      width: 180,
      sorter: (a: any, b: any) => tableSorter(a, b, 'unitPrice'),
      render: (text: number | any) => {
        return <span>{text ? priceFormat(text) : `0.00`}</span>;
      },
    },
    {
      title: '物料简介',
      dataIndex: 'paramsInfo',
      key: 'paramsInfo',
      width: 150,
      render: (text: string) => renderTableText(text, true),
    },
  ];
};

export { updateSourceColumns, updateTempColumns, contractMainTableColumns, openCounterpartColumns, materialTableConfig, openPartyColumns };
